package cn.stylefeng.guns.sys.core.wechatpay;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.core.io.ClassPathResource;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.payments.nativepay.model.CloseOrderRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByIdRequest;
import com.wechat.pay.java.service.payments.nativepay.model.QueryOrderByOutTradeNoRequest;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
import com.wechat.pay.java.service.refund.model.Refund;


public class WechatPayUtils {

	/** appId */
	public static String appId = "wxce885fae994382de";
	/** 商户号 */
	  public static String merchantId = "1633427241";
	  /** 商户API私钥路径 */
	  public static String privateKeyPath = "apiclient_key.pem";
	  /** 商户证书序列号 */
	  public static String merchantSerialNumber = "1810D512F344E9AAD5EF3264662650BFA499424D";
	  /** 商户APIV3密钥 */
	  public static String apiV3Key = "930686F786E3B1C0FDB6294FD847C9AD";

	  
	  public static NativePayService naservice;
	  
	  public static RefundService reservice;
	  
	  
	  public static PrepayResponse prepay (PrepayRequest request) throws IOException  {
		  ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);
	      String privateKeyFromPath = resourcePrivateKey.getFile().getPath();
		  // 初始化商户配置
		  Config config = new RSAAutoCertificateConfig.Builder()
		          .merchantId(merchantId)
		          // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
		          .privateKeyFromPath(privateKeyFromPath)
		          .merchantSerialNumber(merchantSerialNumber)
		          .apiV3Key(apiV3Key)
		          .build();

		  // 初始化服务
		  naservice = new NativePayService.Builder().config(config).build();
		  
		  request.setAppid(appId);
		  request.setMchid(merchantId);
		  return naservice.prepay(request);
	  }
	  
	  
	  /** 关闭订单 
	 * @throws IOException */
	  public static void closeOrder(CloseOrderRequest request) throws IOException {
		  ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);
	      String privateKeyFromPath = resourcePrivateKey.getFile().getPath();
		// 初始化商户配置
		  Config config = new RSAAutoCertificateConfig.Builder()
		          .merchantId(merchantId)
		          // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
		          .privateKeyFromPath(privateKeyFromPath)
		          .merchantSerialNumber(merchantSerialNumber)
		          .apiV3Key(apiV3Key)
		          .build();

		  // 初始化服务
		  naservice = new NativePayService.Builder().config(config).build();
	    // 调用接口
		  request.setMchid(merchantId);
		  naservice.closeOrder(request);
	  }
	  
	  /** 微信支付订单号查询订单 
	 * @throws IOException */
	  public static Transaction queryOrderById(QueryOrderByIdRequest request) throws IOException {
		  ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);
	      String privateKeyFromPath = resourcePrivateKey.getFile().getPath();
		// 初始化商户配置
		  Config config = new RSAAutoCertificateConfig.Builder()
		          .merchantId(merchantId)
		          // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
		          .privateKeyFromPath(privateKeyFromPath)
		          .merchantSerialNumber(merchantSerialNumber)
		          .apiV3Key(apiV3Key)
		          .build();

		  // 初始化服务
		  naservice = new NativePayService.Builder().config(config).build();
	    // 调用接口
		request.setMchid(merchantId);
	    return naservice.queryOrderById(request);
	  }
	  
	  /** 商户订单号查询订单 
	 * @throws IOException */
	  public static Transaction queryOrderByOutTradeNo(QueryOrderByOutTradeNoRequest request) throws IOException {
		  ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);
	      String privateKeyFromPath = resourcePrivateKey.getFile().getPath();
		// 初始化商户配置
		  Config config = new RSAAutoCertificateConfig.Builder()
		          .merchantId(merchantId)
		          // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
		          .privateKeyFromPath(privateKeyFromPath)
		          .merchantSerialNumber(merchantSerialNumber)
		          .apiV3Key(apiV3Key)
		          .build();

		  // 初始化服务
		  naservice = new NativePayService.Builder().config(config).build();
	    // 调用接口
		  request.setMchid(merchantId);
	    return naservice.queryOrderByOutTradeNo(request);
	  }
	  
	  
	  /** 退款申请 
	 * @throws IOException */
	  public static Refund create(CreateRequest request) throws IOException {
		  ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);
	      String privateKeyFromPath = resourcePrivateKey.getFile().getPath();
		// 初始化商户配置
		  Config config = new RSAAutoCertificateConfig.Builder()
				  .merchantId(merchantId)
				  // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
				  .privateKeyFromPath(privateKeyFromPath)
				  .merchantSerialNumber(merchantSerialNumber)
				  .apiV3Key(apiV3Key)
				  .build();

		  // 初始化服务
		  reservice = new RefundService.Builder().config(config).build();
	      // 调用接口
	      return reservice.create(request);
	  }
	  /** 查询单笔退款（通过商户退款单号） 
	 * @throws IOException */
	  public static Refund queryByOutRefundNo(QueryByOutRefundNoRequest request) throws IOException {
		  ClassPathResource resourcePrivateKey = new ClassPathResource(privateKeyPath);
	      String privateKeyFromPath = resourcePrivateKey.getFile().getPath();
		// 初始化商户配置
		  Config config = new RSAAutoCertificateConfig.Builder()
				  .merchantId(merchantId)
				  // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
				  .privateKeyFromPath(privateKeyFromPath)
				  .merchantSerialNumber(merchantSerialNumber)
				  .apiV3Key(apiV3Key)
				  .build();

		  // 初始化服务
		  reservice = new RefundService.Builder().config(config).build();
		  // 调用接口
		  return reservice.queryByOutRefundNo(request);
	  }
}
